package com.demostic.demostic_test.activity;

import android.content.Context;
import android.content.Intent;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.appcompat.app.AlertDialog;

import com.accloud.cloudservice.AC;
import com.accloud.cloudservice.ACDeviceActivator;
import com.accloud.cloudservice.PayloadCallback;
import com.accloud.service.ACDeviceBind;
import com.accloud.service.ACException;
import com.accloud.service.ACUserDevice;
import com.badoo.mobile.util.WeakHandler;
import com.demostic.demostic_test.R;
import com.demostic.demostic_test.base.BaseActivity;
import com.demostic.demostic_test.ui.RoundProgressBar;
import com.demostic.demostic_test.utils.MyLog;
import com.demostic.demostic_test.utils.ToastUtils;
import com.demostic.demostic_test.utils.WifiUtils;

import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.TimeUnit;

public class ThirdApActivity extends BaseActivity implements View.OnClickListener {
    public static final int QUERY_DEVICE_STATUS = 20;
    private static final int SET_PROGRESSBAR = 21;
    public static final int TAG_BIND_FAIL = 18;
    public static final int TAG_START_CONNECT = 19;
    final int STATE_BOUND_SUC = 21;
    final String TAG = ThirdApActivity.class.getSimpleName();
    ACDeviceActivator activator;
    AlertDialog alertDialog;
    private Button btn_retry;
    Context context;
    private WeakHandler handler = new WeakHandler(new Handler.Callback() {
        /* class com.demostic.demostic_test.activity.ThirdApActivity.AnonymousClass1 */

        @Override // android.os.Handler.Callback
        public boolean handleMessage(Message msg) {
            switch (msg.what) {
                case 19:
                    if (!ThirdApActivity.this.isCanceled) {
                        ThirdApActivity.this.timer.start();
                        if (WifiUtils.isWifiConnected_()) {
                            ThirdApActivity.this.bindDevice(ThirdApActivity.this.physicalId);
                        } else {
                            WifiUtils.connectToAp_(ThirdApActivity.this.wifiManager, ThirdApActivity.this.homeWifi, ThirdApActivity.this.pass, ThirdApActivity.this.getCipherType(ThirdApActivity.this.homeWifi));
                            ThirdApActivity.this.bindDevice(ThirdApActivity.this.physicalId);
                        }
                    }
                case 18:
                    if (ThirdApActivity.this.isTimeOut) {
                        ThirdApActivity.this.tv_title.setText(ThirdApActivity.this.getString(R.string.ap_bind_failed));
                        ThirdApActivity.this.progressbar.setVisibility(8);
                        ThirdApActivity.this.thdap_note1.setVisibility(8);
                        ThirdApActivity.this.thdap_note2.setVisibility(8);
                        ThirdApActivity.this.btn_retry.setVisibility(0);
                        ThirdApActivity.this.rl_tips_failed.setVisibility(0);
                        if (!ThirdApActivity.this.isCanceled) {
                            ToastUtils.showToast(ThirdApActivity.this.context, ThirdApActivity.this.getString(R.string.main_aty_bind_fail));
                        }
                        ThirdApActivity.this.isBindFailed = true;
                        ThirdApActivity.this.sendtimer.cancel();
                        break;
                    } else {
                        ThirdApActivity.this.bindDevice(ThirdApActivity.this.physicalId);
                        break;
                    }
                case 20:
                    ThirdApActivity.this.isDeviceBound(ThirdApActivity.this.mUserDevice);
                    break;
                case 21:
                    int progress = ((Integer) msg.obj).intValue();
                    if (ThirdApActivity.this.progressbar != null) {
                        ThirdApActivity.this.progressbar.setProgress(progress);
                        if (progress == 100) {
                            ThirdApActivity.this.sendtimer.cancel();
                            break;
                        }
                    }
                    break;
            }
            return false;
        }
    });
    String homeWifi;
    ImageView image_back;
    private boolean isBindFailed;
    private boolean isBindSuccess;
    boolean isCanceled;
    private boolean isFirst;
    boolean isTimeOut;
    ACUserDevice mUserDevice;
    String pass;
    String physicalId;
    private RoundProgressBar progressbar;
    private int query = 0;
    private RelativeLayout rl_tips_failed;
    int sendp = 0;
    TimerTask sendtask;
    Timer sendtimer;
    String subdomain;
    private TextView thdap_note1;
    private TextView thdap_note2;
    CountDownTimer timer = new CountDownTimer(60000, 1000) {
        /* class com.demostic.demostic_test.activity.ThirdApActivity.AnonymousClass2 */

        @Override // android.os.CountDownTimer
        public void onTick(long millisUntilFinished) {
        }

        @Override // android.os.CountDownTimer
        public void onFinish() {
            ThirdApActivity.this.isTimeOut = true;
            ThirdApActivity.this.handler.sendEmptyMessage(18);
        }
    };
    private TextView tv_title;
    WifiManager wifiManager;

    static /* synthetic */ int access$1008(ThirdApActivity x0) {
        int i = x0.query;
        x0.query = i + 1;
        return i;
    }

    @Override // com.demostic.demostic_test.base.BaseActivity, android.support.v7.app.AppCompatActivity, android.support.v4.app.FragmentActivity, android.app.Activity
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ap_third);
        initView();
        initData();
        toConnect();
    }

    private void initView() {
        this.context = this;
        this.isFirst = true;
        this.tv_title = (TextView) findViewById(R.id.tv_ap_title);
        this.progressbar = (RoundProgressBar) findViewById(R.id.apthd_pb);
        this.rl_tips_failed = (RelativeLayout) findViewById(R.id.rl_tips_failed);
        this.thdap_note1 = (TextView) findViewById(R.id.thdap_note1);
        this.thdap_note2 = (TextView) findViewById(R.id.thdap_note2);
        this.btn_retry = (Button) findViewById(R.id.btn_retry);
        this.btn_retry.setOnClickListener(this);
    }

    private void initData() {
        Bundle bundle;
        this.wifiManager = (WifiManager) getApplicationContext().getSystemService("wifi");
        Intent intent = getIntent();
        if (intent != null && (bundle = intent.getExtras()) != null) {
            this.physicalId = bundle.getString("physicalId");
            this.subdomain = bundle.getString("subdomain");
            this.homeWifi = bundle.getString("homeWifi");
            this.pass = bundle.getString("pass");
        }
    }

    private void toConnect() {
        this.isCanceled = false;
        this.isTimeOut = false;
        setWifiToAp(null, this.homeWifi, this.pass);
        setVisible();
        sendProgressTimer(100);
        this.handler.sendEmptyMessageDelayed(19, 15000);
    }

    public void sendProgressTimer(final int progress) {
        this.sendtimer = new Timer();
        this.sendtask = new TimerTask() {
            /* class com.demostic.demostic_test.activity.ThirdApActivity.AnonymousClass3 */

            @Override // java.util.TimerTask, java.lang.Runnable
            public void run() {
                MyLog.e(ThirdApActivity.this.TAG, "sengProgressBar:" + ThirdApActivity.this.sendp + "," + ThirdApActivity.this.isBindSuccess);
                if (ThirdApActivity.this.sendp <= progress) {
                    if (ThirdApActivity.this.isBindSuccess) {
                        ThirdApActivity.this.sendp = 100;
                    }
                    Message msg = new Message();
                    msg.what = 21;
                    msg.obj = Integer.valueOf(ThirdApActivity.this.sendp);
                    ThirdApActivity.this.handler.sendMessage(msg);
                    ThirdApActivity.this.sendp++;
                }
            }
        };
        this.sendtimer.schedule(this.sendtask, 0, 200);
    }

    @Override // android.view.View.OnClickListener
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_retry /* 2131755230 */:
                if (this.btn_retry.getText().toString().equals(getString(R.string.ap_retry))) {
                    Intent intent = new Intent(this, FirstApActivity.class);
                    intent.setFlags(67108864);
                    intent.putExtra("subdomain", this.subdomain);
                    startActivity(intent);
                }
                stop();
                return;
            default:
                return;
        }
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void setBtStatus(int curStatus) {
        switch (curStatus) {
            case 21:
                if (this.mUserDevice != null) {
                    this.isBindSuccess = true;
                    this.tv_title.setText(getString(R.string.ap_bind_suc));
                    this.progressbar.setVisibility(8);
                    this.thdap_note2.setVisibility(8);
                    this.thdap_note1.setText(getString(R.string.ap_bind_suc1));
                    this.thdap_note1.setTextSize(20.0f);
                    stopAbleLink();
                    Intent intent = new Intent(this, FourthApActivity.class);
                    intent.putExtra("subdomain", this.subdomain);
                    intent.putExtra("physicalId", this.mUserDevice.getPhysicalDeviceId());
                    intent.putExtra("deviceId", this.mUserDevice.getDeviceId());
                    startActivity(intent);
                    return;
                }
                return;
            default:
                return;
        }
    }

    private void setWifiToAp(ACDeviceActivator activator2, String ssid, String pass2) {
        AC.deviceActivator(15).startApLink(ssid, pass2, (int) TimeUnit.SECONDS.toMillis(10), new PayloadCallback<Boolean>() {
            /* class com.demostic.demostic_test.activity.ThirdApActivity.AnonymousClass4 */

            public void success(Boolean aBoolean) {
                MyLog.e(ThirdApActivity.this.TAG, "setWifiToAp success Boolean==:" + aBoolean);
            }

            @Override // com.accloud.cloudservice.BaseCallback
            public void error(ACException e) {
                MyLog.e(ThirdApActivity.this.TAG, "setWifiToAp error Boolean==:" + e.toString());
            }
        }, new PayloadCallback<ACDeviceBind>() {
            /* class com.demostic.demostic_test.activity.ThirdApActivity.AnonymousClass5 */

            public void success(ACDeviceBind acDeviceBind) {
                MyLog.e(ThirdApActivity.this.TAG, "setWifiToAp success ACDeviceBind==:" + acDeviceBind.getPhysicalDeviceId());
            }

            @Override // com.accloud.cloudservice.BaseCallback
            public void error(ACException e) {
                MyLog.e(ThirdApActivity.this.TAG, "setWifiToAp error ACDeviceBind==:" + e.toString());
            }
        });
    }

    public void bindDevice(String physicalId2) {
        AC.bindMgr().bindDevice(this.subdomain, physicalId2, "", new PayloadCallback<ACUserDevice>() {
            /* class com.demostic.demostic_test.activity.ThirdApActivity.AnonymousClass6 */

            public void success(ACUserDevice userDevice) {
                MyLog.e(ThirdApActivity.this.TAG, "bindDevice success :" + userDevice.toString());
                ThirdApActivity.this.mUserDevice = userDevice;
                ThirdApActivity.this.handler.sendEmptyMessageDelayed(20, 5000);
            }

            @Override // com.accloud.cloudservice.BaseCallback
            public void error(ACException e) {
                MyLog.e(ThirdApActivity.this.TAG, "bindDevice error " + e.toString());
                ThirdApActivity.this.handler.sendEmptyMessageDelayed(18, 1000);
            }
        });
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void isDeviceBound(ACUserDevice acUserDevice) {
        AC.bindMgr().isDeviceBound(acUserDevice.getPhysicalDeviceId(), new PayloadCallback<Boolean>() {
            /* class com.demostic.demostic_test.activity.ThirdApActivity.AnonymousClass7 */

            public void success(Boolean aBoolean) {
                MyLog.e(ThirdApActivity.this.TAG, "isDeviceBound success:" + aBoolean);
                ThirdApActivity.access$1008(ThirdApActivity.this);
                if (aBoolean.booleanValue()) {
                    ThirdApActivity.this.stop();
                    if (ThirdApActivity.this.isFirst) {
                        ThirdApActivity.this.setBtStatus(21);
                        ThirdApActivity.this.isFirst = false;
                        return;
                    }
                    return;
                }
                ThirdApActivity.this.reBoundDevice(ThirdApActivity.this.query);
            }

            @Override // com.accloud.cloudservice.BaseCallback
            public void error(ACException e) {
                MyLog.e(ThirdApActivity.this.TAG, "isDeviceBound error:" + e.toString());
                ThirdApActivity.access$1008(ThirdApActivity.this);
                ThirdApActivity.this.reBoundDevice(ThirdApActivity.this.query);
            }
        });
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void reBoundDevice(int query2) {
        if (query2 < 3) {
            this.handler.sendEmptyMessageDelayed(18, 1000);
            return;
        }
        this.timer.cancel();
        this.isTimeOut = true;
        this.handler.sendEmptyMessageDelayed(18, 1000);
    }

    private void setVisible() {
        this.progressbar.setVisibility(0);
        this.thdap_note1.setVisibility(0);
        this.thdap_note2.setVisibility(0);
    }

    public void stop() {
        this.sendtimer.cancel();
        this.timer.cancel();
        this.isCanceled = true;
        this.isTimeOut = true;
        stopAbleLink();
        if (SecondApActivity.cm != null && Build.VERSION.SDK_INT >= 23) {
            SecondApActivity.cm.bindProcessToNetwork(null);
            if (SecondApActivity.callback != null) {
                SecondApActivity.cm.unregisterNetworkCallback(SecondApActivity.callback);
            }
        }
    }

    public void stopAbleLink() {
        if (this.activator != null && this.activator.isAbleLink()) {
            this.activator.stopAbleLink();
        }
    }

    public int getCipherType(String ssid) {
        int type = 0;
        for (ScanResult scResult : this.wifiManager.getScanResults()) {
            if (!TextUtils.isEmpty(scResult.SSID) && scResult.SSID.equals(ssid)) {
                String capabilities = scResult.capabilities;
                if (!TextUtils.isEmpty(capabilities)) {
                    if (capabilities.contains("WPA") || capabilities.contains("wpa")) {
                        type = 2;
                    } else if (capabilities.contains("WEP") || capabilities.contains("wep")) {
                        type = 3;
                    } else {
                        type = 1;
                    }
                }
            }
        }
        return type;
    }

    @Override // android.support.v4.app.FragmentActivity, android.app.Activity
    public void onBackPressed() {
        if (!this.isBindFailed) {
            stop();
            finish();
        }
    }
}
